import { _decorator, Component, find, instantiate, Node, Prefab, Vec3 } from 'cc';
import { ModuleBase } from '../../../FrameWork3.8.6/FrameWork/Module/ModuleBase';
import { Player } from '../Object/Player';
import { Emitter } from '../../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { GameManager } from '../Manager/GameManager';
import { ResMgr } from '../../../FrameWork3.8.6/FrameWork/Res/ResMgr';
import { BundleConfig } from '../../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
import { FollowCamera } from '../Object/FollowCamera';
const { ccclass, property } = _decorator;

@ccclass('PlayerLayer')
export class PlayerLayer extends ModuleBase {
    //存储生成的玩家
    private _player:Player=null;

    get player(){
        return this._player;
    }
    set player(player:Player){
        this._player=player;
    } 
    
    
    onInit(...rest: any[]): void {
        //每次到达这里时新建一个玩家出来
        let playerDt=GameManager.instance.playerDt;
        let playerNode=instantiate(ResMgr.instance.getRes<Prefab>(BundleConfig.Game.name,playerDt.prefabName));
        playerNode.parent=this.node;
        this._player=playerNode.getComponent(Player);
        //调用玩家的初始化方法
        this._player.onInit(playerDt,1);

        //注册消息
        Emitter.instance.on("walk",this.normalWalk,this)
        Emitter.instance.on("tp",this.normalTp,this)
        Emitter.instance.on("end",this.endMove,this)
    }

    normalWalk(dirName:string){
        this._player.normalWalk(dirName);
    }

    normalTp(dirName:string){
        this._player.normalTp(dirName);
    }

    endMove(dirName:string){
        this.player.endMove(dirName);
    }


    onUpdate(dt: number, ...rest: any[]): void {
        this.player.onUpdate(dt);
    }


    //回收该玩家的方法
    removePlayer(){
        GameManager.instance.timePause=true;
        //先将相机取消跟随
        //获取Camera脚本
        let camera=find("Canvas/FollowCamera").getComponent(FollowCamera);
        //这里通过发送消息给相机
        Emitter.instance.emit("RemoveCamera",camera);
    }

}


